<?php
/**
 * 
 * @author szm
 *
 */

class Dialogue_model extends CI_Model {
    
    
    public function __construct(){
        parent::__construct();
        $this->load->database();
    }
    public  function test(){
    
        return "testing";
    }

    public function getSampleSentanceIds($lessonIds){
        if(empty($lessonIds)){
            return false;
        }
        $sql="select sample_sentence_id from sample_sentences as ss where lesson_id  in ({$lessonIds}) ";
        $dbSource=$this->db->query($sql);
        $resultArr=$dbSource->result_array();
        return $resultArr;
    }
    
    private function parseWhereArr($fieldValues){

        if(!is_array($fieldValues)){
            $fieldValues=array($fieldValues);
        }
        $fieldValues=implode(",",$fieldValues);
    
        return $fieldValues;
    }
/**
 * 
 * @param mixed $lessonIds
 * @return array
 */
    public function getDialogues($lessonIds=0){
        if(empty($lessonIds)){
            return array();
        }
        if(!is_array($lessonIds)){
            $lessonIds=array($lessonIds);
        }
        $lessonIds=implode(",",$lessonIds);
		try{
			$this->db->select('*');
			$this->db->from('dialogue as ds');
			$this->db->join('sentence as ss', 'ds.sentence_id  = ss.sentence_id ', 'inner');
			$this->db->join('sentence_source_1 as sc', 'sc.sentence_id  = ss.sentence_id ', 'left');
			//$this->db->join('sentence_target_1 as st', 'st.sentence_id  = ss.sentence_id ', 'left');
			$this->db->where_in('ss.lesson_id',$lessonIds);
			$this->db->order_by('ds.display_order','asc');
			$result=$this->db->get();
			$rs=$result->result_array();
            return $rs;
	     }catch(Exception $e){
            return array();
         }

    }
	
    public function getDialoguesbk($lessonIds=0){

        if(empty($lessonIds)){
            return false;
        }
        if(!is_array($lessonIds)){
            $lessonIds=array($lessonIds);
        }
        $lessonIds=implode(",",$lessonIds);
        $sql="select * from dialogue_sentences as ds
			left join sample_sentences  as ss
        	on ds.sample_sentence_id  = ss.sample_sentence_id 
			where ss.lesson_id in ($lessonIds) order by display_order asc";
	     try{
            $dbSource=$this->db->query($sql);
            $resultArr=$dbSource->result_array();
            return $resultArr;
	     }catch(Exception $e){
            return $sql;
         }

    }
    public function listAllDialogues(){

    }
    
    public function getDialogue(){

    }
    
    public function saveDialogue(){


    }
    
    public function removeDialogue(){

    }
}

/*    public function getDialogues($lessonIds=0){
        if(empty($lessonIds)){
            return array();
        }
        if(!is_array($lessonIds)){
            $lessonIds=array($lessonIds);
        }
        $lessonIds=implode(",",$lessonIds);
		try{
			$this->db->select('*');
			$this->db->from('dialogue_sentences as ds');
			$this->db->join('sample_sentences as ss', 'ds.sample_sentence_id  = ss.sample_sentence_id ', 'left');
			$this->db->where_in('ss.lesson_id',$lessonIds);
			$this->db->order_by('ds.display_order','asc');
			$result=$this->db->get();
			$rs=$result->result_array();
			fb::dump('getDialogues',$rs);
            return $rs;
	     }catch(Exception $e){
            return array();
         }

    }*/


